//
//  THJForgetPswVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/4/12.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJForgetPswVC.h"
#import "NSString+String.h"

@interface THJForgetPswVC ()
{
    NSString * token;
}
@end

@implementation THJForgetPswVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden =  YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    self.title = @"忘记登录密码";
    self.secretTF1.secureTextEntry = YES;
    self.secretTF2.secureTextEntry = YES;
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    self.verifyTF.keyboardType = UIKeyboardTypeNumberPad;
//    [self setTextFieldLeftImageView:self.phoneTF leftImageName:@"手机-2"];
//    [self setTextFieldLeftImageView:self.secretTF1 leftImageName:@"锁"];
//    [self setTextFieldLeftImageView:self.secretTF2 leftImageName:@"锁"];
//    [self setTextFieldLeftImageView:self.verifyTF leftImageName:@"valid"];
    
//    self.getVerifyBtn.layer.borderWidth = 1;
//    self.getVerifyBtn.layer.borderColor = [COLOR(220, 220, 220, 1) CGColor];

        // Do any additional setup after loading the view from its nib.
}
- (IBAction)submitBtnClicked:(id)sender {
    BOOL isOrNo = [NSString isMobileNumber:self.phoneTF.text];
    if (!isOrNo) {
        [self HUDShowMsg:@"请输入正确手机号码" AfterDelayDimiss:1];
        return;
    }
    if(self.verifyTF.text.length==0){
        [self HUDShowMsg:@"请输入验证码" AfterDelayDimiss:1];
        return;
    }
    if(self.secretTF1.text.length==0){
        [self HUDShowMsg:@"请输入密码" AfterDelayDimiss:1];
        return;
    }
    if(self.secretTF2.text.length==0){
        [self HUDShowMsg:@"请再次输入密码" AfterDelayDimiss:1];
        return;
    }
    if(![self.secretTF1.text isEqual:self.secretTF2.text]){
        [self HUDShowMsg:@"两次输入密码不一致" AfterDelayDimiss:1];
        return;
    }
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=index&r=index&m=forget"];
    NSDictionary * paraDic = @{
                               @"code":self.verifyTF.text,
                               @"mobile":self.phoneTF.text,
                               @"password":self.secretTF1.text,
                               @"repassword":self.secretTF2.text
                               };
    __weak typeof(self) weakself = self;
    [THJNetWork postDataFromUrl:urlstr parameters:paraDic Token:token callback:^(NSDictionary* receiveData, NSError *error) {
          [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [[NSUserDefaults standardUserDefaults] setObject:weakself.secretTF1.text forKey:@"password"];
            [weakself performSelector:@selector(dismissSelf) withObject:nil afterDelay:1];
        }
    }];
}
-(void)dismissSelf{
     [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)getCodeBtnClicked:(id)sender {
    BOOL isPhoneNum = [NSString isMobileNumber:self.phoneTF.text];
    if (!isPhoneNum) {
        [self HUDShowMsg:@"请输入正确手机号" AfterDelayDimiss:1];
        return;
    }
    [self startWithSecond:59];
    NSString * urlStr = [NSString stringWithFormat:@"%@index.php?f=index&r=index&m=code&mobile=%@",THJUrlDomain,self.phoneTF.text];
    __weak typeof(self) weakself = self;
    [THJNetWork getNetDataFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
        if (!error) {
            NSLog(@"yanzhegnma-%@",receiveData);
            if ([receiveData[@"status"] isEqual:@"200"]) {
                token = receiveData[@"data"];
                [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            }{
                [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            }
        }
    }];
}
//按钮开始倒计时
-(void)startWithSecond:(NSInteger)second{
    __weak typeof(self) weakself = self;
    __block NSInteger timeout=second; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [weakself.getVerifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                weakself.getVerifyBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
        
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                weakself.getVerifyBtn.titleLabel.text = strTime;
                [weakself.getVerifyBtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                weakself.getVerifyBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (IBAction)dismiss:(id)sender {
    [self pop];
}
@end
